package com.yanqu.road.server.logic.activity.condition.flowerstudy;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.FlowerStudyRankArgs;
import com.yanqu.road.entity.activity.flowerstudy.config.FlowerStudyConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.server.manger.activity.flowerstudy.FlowerStudyMgr;

import java.math.BigInteger;
import java.util.Date;

public class FlowerStudyLocalScoreRankCondition extends BaseNewRankActivityCondition {
    public FlowerStudyLocalScoreRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        FlowerStudyRankArgs arg = (FlowerStudyRankArgs)object;
        if (arg.getScore() <= 0){          //0分别上榜了
            return;
        }
        FlowerStudyConfig config = FlowerStudyMgr.getConfig();
        if (config == null){
            return;
        }
        Date now = new Date();
        if (now.before(config.getLocalExamBeginDate()) || now.after(config.getLocalExamEndDate())){
            return;
        }
        if (arg.getScore() == getValue().longValue() && String.valueOf(arg.getTime()).equals(getParam())){
            return;
        }
        setParam(String.valueOf(arg.getTime()));
        setValue(BigInteger.valueOf(arg.getScore()));

        //重新生成排行榜
        rankChange();
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.FlowerStudyScoreLocalRankCondition.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.FlowerStudyScoreLocalRankCondition.getValue(), handle);
    }
}
